package com.com.effectivejava.commonmethods.hashcode;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by User on 11/07/2014.
 */
public class HashcodeProblemSolved {

        public static void main(String[] args) {
        Employee2 e1 = new Employee2(1, "A");
        Employee2 e2 = new Employee2(2, "B");
        Employee2 e3 = new Employee2(2, "B");

        Set<Employee2> employeeSet = new HashSet<Employee2>();

        employeeSet.add(e1);
        employeeSet.add(e2);
        employeeSet.add(e3);

        //it prints 2, cause of hashCode() method in Employee class.
        System.out.println("employeeSet.size() = " + employeeSet.size());
    }


}

    class Employee2 {
        private final String name;
        private final int id;

        Employee2(int id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public boolean equals(Object obj) {
            System.out.println(" obj.toString() = " + obj.toString());
            if (obj == null) {
                return false;
            }

            if (obj == this)
                return true;

            if (!(obj instanceof Employee2))
                return false;

            Employee2 e = (Employee2) obj;
            return this.id == e.id;
        }

        @Override
        public int hashCode() {
            System.out.println("inside hash code = ");
            return this.id;
        }
    }


